use clap::Parser;
use rusqlite::Result;
use sqlite_tools::SqliteTools;
use std::path::Path;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
    /// Name of the person to greet
    #[arg(short, long, help = "数据库路径")]
    path: String,

    #[arg(short, long, help = "打印分割符",default_value_t=String::from("|"))]
    separator: String,

    #[arg( long, help = "打印表头装饰",default_value_t=false)]
    header_decorator: bool,
    
    #[arg(short, long,help = "sql查询语句,用\"包裹。不存在时，显示数据库表信息", default_value_t = String::from(""))]
    query: String,
}

fn main() -> Result<()> {
    let args = Args::parse();
    if Path::new(&args.path).exists() != true {
        println!("文件不存在:{}", &args.path);
        return Ok(());
    }
    let sqlite_tools = SqliteTools::builder(&args.path);
    // let if table_name = args.table_name
    let table_infos = sqlite_tools.tables_info();
    if args.query.is_empty() {
        for table_info in &table_infos {
            println!("{}", table_info);
        }
    } else {
        // let exist = &table_infos
        //     .iter()
        //     .filter(|info| info.tbl_name == args.table_name)
        //     .count()
        //     .eq(&1);
        // .map(|info|&info.name);
        sqlite_tools.query(&args.query,&args.separator,args.header_decorator);
    }
    return Ok(());
}
